window.onload = function () {
    loadSubject()
}
// 加载课程信息的方法
function loadSubject() {
    // 发送Ajax请求获取学生信息
    let xhr = new XMLHttpRequest()
    // 清空之前的数据
    tbody.innerHTML = ""

    // 获取搜索框里的内容
    let subjectName = searchText.value
    /* 
        https://www.bingjs.com:8001/Subject/GetSubjectsConditionPages  根据条件查询+分页  [GET请求]
            参数：
            (
            subjectName:  课程名称  可以为空，支持模糊查询
            pageIndex：页码，默认值是1
            pageSize:  每页数量，默认值是10
            )
    */
    xhr.open("GET", `https://www.bingjs.com:8001/Subject/GetSubjectsConditionPages?pageIndex=${pageIndex.innerHTML}&subjectName=${subjectName}`)
    xhr.send()
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            // 获取数据 总数量 当前页码 每页数量
            let { data, count, pageIndex, pageSize } = JSON.parse(xhr.response)
            console.log(data);
            // 总页数
            let pageCount = Math.ceil(count / pageSize)
            // 显示总页数
            totalPage.innerHTML = pageCount
            data.forEach(a => {
                console.log(a);
                let tr = document.createElement("tr")
                let td_subjectId = document.createElement("td")
                td_subjectId.innerHTML = a.SubjectId
                let td_subjectName = document.createElement("td")
                td_subjectName.innerHTML = a.SubjectName
                let td_classHour = document.createElement("td")
                td_classHour.innerHTML = a.ClassHour
                let td_gradeId = document.createElement("td")
                td_gradeId.innerHTML = a.Grade.GradeId
                let td_gradeName = document.createElement("td")
                td_gradeName.innerHTML = a.Grade.GradeName
                let td_action = document.createElement("td")
                let btn_modify = document.createElement("button")
                btn_modify.innerHTML = "修改"
                let btn_del = document.createElement("button")
                btn_del.innerHTML = "删除"

                td_action.appendChild(btn_modify)
                td_action.appendChild(btn_del)
                tr.appendChild(td_subjectId)
                tr.appendChild(td_subjectName)
                tr.appendChild(td_classHour)
                tr.appendChild(td_gradeId)
                tr.appendChild(td_gradeName)

                tr.appendChild(td_action)
                tbody.appendChild(tr)

                // 修改课程信息
                let btn_modify_tr = document.getElementsByTagName("tbody")[0].children
                console.log(btn_modify_tr)
                let btn_modify_td = btn_modify_tr[0].children
                console.log(btn_modify_td[5].firstElementChild);
                console.log(btn_modify_tr[0].children[5].firstElementChild);
                console.log(btn_modify_tr[0].firstElementChild);
                for (let i = 0; i < btn_modify_tr.length; i++) {
                    btn_modify_tr[i].children[5].firstElementChild.onclick = function () {
                        let modify_box = document.getElementsByClassName("modify_box")[0]
                        console.log(modify_box);
                        modify_box.style.display = "block"
                        let modify_tbody_tr = document.getElementById("modify_tbody").children
                        console.log(modify_tbody_tr);
                        let modify_subjectId = modify_tbody_tr[0].firstElementChild.nextElementSibling.firstElementChild
                        console.log(modify_subjectId);
                        modify_subjectId.value = btn_modify_tr[i].firstElementChild.innerHTML

                        let modify_subjectName = modify_tbody_tr[1].firstElementChild.nextElementSibling.firstElementChild
                        console.log(modify_subjectName);
                        modify_subjectName.value = btn_modify_tr[i].firstElementChild.nextElementSibling.innerHTML

                        let modify_classHour = modify_tbody_tr[2].firstElementChild.nextElementSibling.firstElementChild
                        console.log(modify_classHour);
                        modify_classHour.value = btn_modify_tr[i].firstElementChild.nextElementSibling.nextElementSibling.innerHTML

                        let modify_gradeId = modify_tbody_tr[3].firstElementChild.nextElementSibling.firstElementChild
                        console.log(modify_gradeId);
                        modify_gradeId.value = btn_modify_tr[i].firstElementChild.nextElementSibling.nextElementSibling.nextElementSibling.innerHTML
                        
                        // 修改按钮点击事件
                        let modify_btn = document.getElementById("modify_subject_submit")
                        console.log(modify_btn)
                        modify_btn.onclick = function () {
                            // let modify_subjectId = modify_tbody_tr[0].firstElementChild.nextElementSibling.firstElementChild.value
                            // console.log(modify_subjectId);
                            let subjectId = modify_tbody_tr[0].firstElementChild.nextElementSibling.firstElementChild.value
                            console.log(subjectId);
                            // btn_modify_tr[i].firstElementChild.innerHTML = modify_subjectId

                            // let modify_subjectName = modify_tbody_tr[1].firstElementChild.nextElementSibling.firstElementChild.value
                            // console.log(modify_subjectName);
                            let subjectName = modify_tbody_tr[1].firstElementChild.nextElementSibling.firstElementChild.value
                            console.log(subjectName);
                            // btn_modify_tr[i].firstElementChild.nextElementSibling.innerHTML = modify_subjectName

                            // let modify_classHour = modify_tbody_tr[2].firstElementChild.nextElementSibling.firstElementChild.value
                            // console.log(modify_classHour);
                            let classHour = modify_tbody_tr[2].firstElementChild.nextElementSibling.firstElementChild.value
                            console.log(classHour);
                            // btn_modify_tr[i].firstElementChild.nextElementSibling.nextElementSibling.innerHTML = modify_classHour

                            // let modify_gradeId = modify_tbody_tr[3].firstElementChild.nextElementSibling.firstElementChild.value
                            // console.log(modify_gradeId);
                            let gradeId = modify_tbody_tr[3].firstElementChild.nextElementSibling.firstElementChild.value
                            console.log(gradeId);
                            // btn_modify_tr[i].firstElementChild.nextElementSibling.nextElementSibling.nextElementSibling.innerHTML = modify_gradeId

                            let modify_obj = {
                                // modify_subjectId,
                                // modify_subjectName,
                                // modify_classHour,
                                // modify_gradeId
                                subjectId,
                                subjectName,
                                classHour,
                                gradeId
                            }
                            let modify_json = JSON.stringify(modify_obj)
                            let xhr = new XMLHttpRequest()
                            /* 
                                https://www.bingjs.com:8001/Subject/Update 修改课程  [POST请求]
                                    参数：
                                    (
                                        subjectId：课程编号
                                        subjectName：课程名称
                                        classHour：课时
                                        gradeId：年级编号
                                    )
                            */
                            xhr.open("POST", `https://www.bingjs.com:8001/Subject/Update`)
                            // xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
                            xhr.setRequestHeader("Content-Type", "application/json")
                            xhr.send(modify_json)
                            xhr.onreadystatechange = function () {
                                if (xhr.readyState === 4 && xhr.status === 200) {
                                    let modify_obj = JSON.parse(xhr.response)
                                    console.log(modify_obj);
                                    if (modify_obj) {
                                        loadSubject()
                                        alert("操作成功")
                                    }
                                }
                            }
                        }
                        // 取消按钮点击事件
                        let modify_btn_cancel_btn = document.getElementById("modify_subject_cancel")
                        modify_btn_cancel_btn.onclick = function(){
                            modify_box.style.display = "none"
                        }
                    }
                }


                // 删除课程信息
                btn_del.onclick = function () {
                    if (!confirm(`你确定要删除${a.SubjectId}${a.SubjectName}嘛`)) {
                        return
                    }
                    // 通过Ajax 获取数据 并删除
                    let xhr = new XMLHttpRequest()
                    xhr.open("POST", "https://www.bingjs.com:8001/Subject/Delete")
                    // 删除数据 SubjectID
                    // ajax 请求报 415 ，请求参数类型与后台接收参数未对应
                    // 解决方式：添加contentType，同时将data参数转为json字符串即可
                    // xhr.setRequestHeader ("content-type", "application/x-www-form-urlencoded" )
                    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
                    console.log(a.SubjectId);
                    xhr.send("subjectId=" + a.SubjectId)
                    xhr.onreadystatechange = function () {
                        if (xhr.readyState === 4 && xhr.status === 200) {
                            let obj = JSON.parse(xhr.response)
                            if (obj) {
                                alert("删除成功")
                                location.href = "../html/searchSubject.html"
                            }
                        }
                    }
                }
            })
        }
    }
}


// 首页按钮点击事件
first.onclick = function () {
    // 将页码设置为1
    if (pageIndex.innerHTML == 1) {
        return
    }
    pageIndex.innerHTML = 1
    // 调用加载课程信息的方法
    loadSubject()
}

// 下一页按钮点击事件
next.onclick = function () {
    // 获取当前页数
    let index = parseInt(pageIndex.innerHTML)
    // 获取总页数
    let indexCount = parseInt(totalPage.innerHTML)
    // 如果当前页小于总页数
    if (index < indexCount) {
        index++
        pageIndex.innerHTML = index
        loadSubject()
    }
}

// 上一页按钮点击事件
prev.onclick = function () {
    // 获取当前页数
    let index = parseInt(pageIndex.innerHTML)
    // 如果当前页数大于1
    if (index > 1) {
        index--
        pageIndex.innerHTML = index
        loadSubject()
    }
}

// 尾页按钮点击事件
last.onclick = function () {
    // 获取当前页数
    let index = parseInt(pageIndex.innerHTML)
    // 获取总页数
    let indexCount = parseInt(totalPage.innerHTML)
    // 当前页码设置为总页码
    if (index != indexCount) {
        pageIndex.innerHTML = totalPage.innerHTML
        loadSubject()
    }

}

// 搜索按钮点击事件
searchBtn.onclick = function () {
    // 调用加载学生信息
    loadSubject()
}



// 根据课程编号查询单个课程信息
// 查询按钮点击事件
searchByIDBtn.onclick = function () {
    console.log(location);
    // 发送Ajax请求获取学生信息
    let xhr = new XMLHttpRequest()
    // 清空之前的数据
    searchByID_tbody.innerHTML = ""
    // 获取搜索框里的内容
    let subjectID = searchTextByID.value
    if (subjectID === "") {
        alert("课程编号不可以为空！请输入课程编号！")
    }
    /* 
          https://www.bingjs.com:8001/Subject/GetSubjectById  根据课程编号查询一个课程  [GET请求]
            参数：
            (
            subjectId 课程编号
            )
    */
    xhr.open("GET", `https://www.bingjs.com:8001/Subject/GetSubjectById?SubjectId=${subjectID}`)
    xhr.send()
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            let str = JSON.parse(xhr.response)
            console.log(str);
            let c_tr = document.createElement("tr")
            let c_td_subjectId = document.createElement("td")
            c_td_subjectId.innerHTML = str.SubjectId
            let c_td_subjectName = document.createElement("td")
            c_td_subjectName.innerHTML = str.SubjectName
            let c_td_classHour = document.createElement("td")
            c_td_classHour.innerHTML = str.ClassHour
            let c_td_gradeId = document.createElement("td")
            c_td_gradeId.innerHTML = str.Grade.GradeId
            let c_td_gradeName = document.createElement("td")
            c_td_gradeName.innerHTML = str.Grade.GradeName

            c_tr.appendChild(c_td_subjectId)
            c_tr.appendChild(c_td_subjectName)
            c_tr.appendChild(c_td_classHour)
            c_tr.appendChild(c_td_gradeId)
            c_tr.appendChild(c_td_gradeName)
            searchByID_tbody.appendChild(c_tr)
        }
    }
}